using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using System.Linq;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;

using System.Linq.Dynamic.Core;
 using Microsoft.EntityFrameworkCore; 
using DualControl.Posts.Authorization;
using DualControl.Posts.DomainServices;
using DualControl.Posts.Dtos;
using DualControl.Posts;
using Abp.Dapper.Repositories;

namespace DualControl.Posts
{
    /// <summary>
    /// Post应用层服务的接口实现方法
    /// </summary>
	[AbpAuthorize(PostAppPermissions.Post)]
    public class PostAppService : DualControlAppServiceBase, IPostAppService
    {
		private readonly IRepository<Post, int> _postRepository;

		private readonly IPostManager _postManager;

        private readonly IDapperRepository<Post> _postDapperRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        public PostAppService(
			IRepository<Post, int> postRepository
			,IPostManager postManager
            ,IDapperRepository<Post> postDapperRepository
        )
		{
			_postRepository = postRepository;
			 _postManager=postManager;
            _postDapperRepository = postDapperRepository;
		}

        //返回所有的岗位信息
        public async Task <List<PostListDto>> GetAllPosts()
        {
            var query = _postRepository.GetAll();
            // TODO:根据传入的参数添加过滤条件

          
            var posts = await query
                    .OrderBy("Name").AsNoTracking()                    
                    .ToListAsync();

            // var postListDtos = ObjectMapper.Map<List <PostListDto>>(posts);
            var postsList = posts.MapTo<List<PostListDto>>();

            return  postsList;  

        }

        public List<Post> GetAllPostsByDapper()
        {
            
            var posts = _postDapperRepository.Query("select * from Posts");
            return posts.ToList();
        }
        public  int GetCount()
        {           
           var num = _postDapperRepository.Query("select * from Posts").Count();
           return num;

        }

        /// <summary>
        /// 获取Post的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public  async  Task<PagedResultDto<PostListDto>> GetPagedPosts(GetPostsInput input)
		{
		    
		    var query = _postRepository.GetAll();
			// TODO:根据传入的参数添加过滤条件
		
			var postCount = await query.CountAsync();
		
			var posts = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();
		
				// var postListDtos = ObjectMapper.Map<List <PostListDto>>(posts);
				var postListDtos =posts.MapTo<List<PostListDto>>();
		
				return new PagedResultDto<PostListDto>(
							postCount,
							postListDtos
					);
		}
		

		/// <summary>
		/// 通过指定id获取PostListDto信息
		/// </summary>
		public async Task<PostListDto> GetPostByIdAsync(EntityDto<int> input)
		{
			var entity = await _postRepository.GetAsync(input.Id);
		
		    return entity.MapTo<PostListDto>();
		}
		
		/// <summary>
		/// MPA版本才会用到的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async  Task<GetPostForEditOutput> GetPostForEdit(NullableIdDto<int> input)
		{
			var output = new GetPostForEditOutput();
			PostEditDto postEditDto;
		
			if (input.Id.HasValue)
			{
				var entity = await _postRepository.GetAsync(input.Id.Value);
		
				postEditDto = entity.MapTo<PostEditDto>();
		
				//postEditDto = ObjectMapper.Map<List <postEditDto>>(entity);
			}
			else
			{
				postEditDto = new PostEditDto();
			}
		
			output.Post = postEditDto;
			return output;
		}
		
		
		/// <summary>
		/// 添加或者修改Post的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdatePost(CreateOrUpdatePostInput input)
		{
		    
			if (input.Post.Id.HasValue)
			{
				await UpdatePostAsync(input.Post);
			}
			else
			{
				await CreatePostAsync(input.Post);
			}
		}
		

		/// <summary>
		/// 新增Post
		/// </summary>
		//[AbpAuthorize(PostAppPermissions.Post_CreatePost)]
		protected virtual async Task<PostEditDto> CreatePostAsync(PostEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增
		
			var entity = ObjectMapper.Map <Post>(input);
		
			entity = await _postRepository.InsertAsync(entity);
			return entity.MapTo<PostEditDto>();
		}
		
		/// <summary>
		/// 编辑Post
		/// </summary>
		//[AbpAuthorize(PostAppPermissions.Post_EditPost)]
		protected virtual async Task UpdatePostAsync(PostEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新
		
			var entity = await _postRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);
		
			// ObjectMapper.Map(input, entity);
		    await _postRepository.UpdateAsync(entity);
		}
		

		
		/// <summary>
		/// 删除Post信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		//[AbpAuthorize(PostAppPermissions.Post_DeletePost)]
		public async Task DeletePost(EntityDto<int> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _postRepository.DeleteAsync(input.Id);
		}
		
		
		
		/// <summary>
		/// 批量删除Post的方法
		/// </summary>
		//[AbpAuthorize(PostAppPermissions.Post_BatchDeletePosts)]
		public async Task BatchDeletePostsAsync(List<int> input)
		{
			//TODO:批量删除前的逻辑判断，是否允许删除
			await _postRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出Post为excel表
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetPostsToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}


		
		//// custom codes 
		
        //// custom codes end

    }
}


 